package com.wan37.client.message.received;

import com.google.protobuf.Message;
import com.wan37.client.Client;
import com.wan37.client.handler.ClientHandler;
import com.wan37.client.service.cache.GuildCache;
import com.wan37.client.service.cache.UserServiceCache;
import com.wan37.protobuf.constant.CommonGuild;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.GameGuild;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.Column;
import java.util.Collections;

/**
 * @author : luoyong
 * @date : 2020-08-04 15:33
 **/
@Component(PtoNum.GUILDRESPONSE + "")
public class GuildResponse implements ReceiveMsg{
    private final Logger logger = LoggerFactory.getLogger(GuildResponse.class);
    @Autowired
    private GuildCache guildCache;
    @Autowired
    private UserServiceCache userServiceCache;
    private ClientHandler clientHandler = ClientHandler.getInstance();
    @Override
    public void execute(Message msg) {
        GameGuild.GuildResponse guildResponse = (GameGuild.GuildResponse) msg;
        int code = guildResponse.getCode();
        switch (code){
            case CommonGuild.GET_ALL:
                if (guildResponse.getSimpleAllGuildList().size() != 0){
                    guildCache.getSimpleAllGuildList().clear();
                    guildCache.getSimpleAllGuildList().addAll(guildResponse.getSimpleAllGuildList());
                    Collections.sort(guildCache.getSimpleAllGuildList(),(s1, s2)->s2.getLevel() - s1.getLevel());
                }
                if (!"".equals(guildResponse.getGuild().getGuildId())){
                    guildCache.setGuild(guildResponse.getGuild());
                    guildCache.setGuildUserList(guildResponse.getGuildUserList());
                    guildCache.setGuildWarehouseList(guildResponse.getGuildWarehouseList());
                    userServiceCache.getUserInMap().get(clientHandler.selfId).setGuildId(guildResponse.getGuild().getGuildId());
                }
                break;
            case CommonGuild.UPDATE_GUILD_USER_JOB:
                guildCache.setGuildUserList(guildResponse.getGuildUserList());
                logger.info("会员职位变更成功，可以通过查看公会基础信息查看！");
                break;
            case CommonGuild.GET_APPLY_USER_ID:
                guildCache.getApplyUserIdList().clear();
                guildResponse.getApplyUserIdList().forEach(stringMsg ->
                        guildCache.getApplyUserIdList().add(stringMsg.getStringId()));
                break;
            case CommonGuild.ADD_GUILD:
                guildCache.setGuild(guildResponse.getGuild());
                guildCache.setGuildUserList(guildResponse.getGuildUserList());
                guildCache.setGuildWarehouseList(guildResponse.getGuildWarehouseList());
                userServiceCache.getUserInMap().get(clientHandler.selfId).setGuildId(guildResponse.getGuild().getGuildId());
                logger.info("公会创建成功！");
                break;
            case CommonGuild.OTHER_DELETE_GUILD:
                //说明是别人退出公会
                logger.info(guildResponse.getString());
                guildCache.setGuildUserList(guildResponse.getGuildUserList());
                break;
            case CommonGuild.SELF_DELETE_GUILD:
                guildCache.setGuild(null);
                guildCache.setGuildUserList(null);
                guildCache.setGuildWarehouseList(null);
                userServiceCache.getUserInMap().get(clientHandler.selfId).setGuildId(null);
                logger.info(guildResponse.getString());
                break;
            case CommonGuild.HEADER_DELETE_GUILD:
                guildCache.setGuild(null);
                guildCache.setGuildUserList(null);
                guildCache.setGuildWarehouseList(null);
                userServiceCache.getUserInMap().get(clientHandler.selfId).setGuildId(null);
                for (GameGuild.SimpleAllGuild simpleAllGuild : guildCache.getSimpleAllGuildList()){
                    if (simpleAllGuild.getGuildId().equals(guildResponse.getGuild().getGuildId())){
                        guildCache.getSimpleAllGuildList().remove(simpleAllGuild);
                        break;
                    }
                }
                guildCache.getApplyUserIdList().clear();
                logger.info(guildResponse.getString());
                break;
            case CommonGuild.ADD_GUILD_USER:
                guildCache.setGuildUserList(guildResponse.getGuildUserList());
                logger.info("有新成员加入，通过查看公会基础信息查看吧！");
                break;
            case CommonGuild.DELETE_GUILD_USER:
                if ("".equals(guildResponse.getGuild().getGuildId())){
                    logger.info(guildResponse.getString());
                }else {
                    guildCache.setGuild(null);
                    guildCache.setGuildUserList(null);
                    guildCache.setGuildWarehouseList(null);
                    userServiceCache.getUserInMap().get(clientHandler.selfId).setGuildId(null);
                    logger.info(guildResponse.getString());
                }
                break;
            case CommonGuild.ERROR:
            case CommonGuild.APPLY_GUILD_USER:
                logger.info("{}", guildResponse.getString());
                break;
            default:
                break;
        }
    }
}
